package com.support.config;

import com.support.annotation.*;
import com.youshang520i.model.SystemUser;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author youshang
 */
public class MyHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
//        parameter.getParameterType().getName().equals(SystemUser.class.getName())
        return parameter.hasParameterAnnotation(SystemUserAnnotation.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        String token = webRequest.getHeader("token");
        SystemUser user = new SystemUser();
        if (!"".equals(token) && null != token) {
            user = new SystemUser();
            user.setUserName("youshang520i -> Annotation !!!");
        }
        return user;
    }
}
